﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;

namespace ECommerce.WebControls.Productos
{
    public class WebPackagesByProducto : System.Web.UI.WebControls.CompositeControl
    {
        private ECommerce.WebControls.ObjetoListItem[] items;
        public ECommerce.WebControls.ObjetoListItem[] Items
        {
            get { return this.items; }
            set { this.items = value; }
        }
        public System.Web.UI.WebControls.Unit textSize;
        public System.Web.UI.WebControls.Unit TextSize
        {
            get { return this.textSize; }
            set { this.textSize = value; }
        }
        public bool checkBoxVisible;
        public bool CheckBoxVisible
        {
            get { return checkBoxVisible; }
            set { checkBoxVisible = value; }
        }
        private string addText;
        public string AddText
        {
            get { return this.addText; }
            set { this.addText = value; }
        }
        private string editText;
        public string EditText
        {
            get { return this.editText; }
            set { this.editText = value; }
        }
        private string editNavigateUrl;
        public string EditNavigateUrl
        {
            get { return this.editNavigateUrl; }
            set { this.editNavigateUrl = value; }
        }
        private string delText;
        public string DelText
        {
            get { return this.delText; }
            set { this.delText = value; }
        }
        private string delImageUrl;
        public string DelImageUrl
        {
            get { return this.delImageUrl; }
            set { this.delImageUrl = value; }
        }
        virtual public void Load(int ident)
        {
            ECommerce.Model.Productos.Package[] Packages = ECommerce.DAO.SQL.Productos.Package.GetPackagesByProductoId(ident);
            this.Load(Packages);
        }
        virtual public void Load(ECommerce.Model.Productos.Package[] Packages)
        {
            List<ECommerce.WebControls.ObjetoListItem> lstItems = new List<ECommerce.WebControls.ObjetoListItem>();
            ECommerce.WebControls.ObjetoListItem Item;
            foreach (ECommerce.Model.Productos.Package Package in Packages)
            {
                string nombre = Package.ToString();
                Item = new ECommerce.WebControls.ObjetoListItem(false, Package.Id.ToString(), nombre, this.AddText, this.editText, this.EditNavigateUrl, this.DelText, this.DelImageUrl);
                Item.TextSize = this.TextSize;
                Item.CheckBoxVisible = this.CheckBoxVisible;
                lstItems.Add(Item);
                Item.lnkAddCommand += new System.Web.UI.WebControls.CommandEventHandler(lnkAdd_Command);
                Item.lnkDelCommand += new System.Web.UI.WebControls.CommandEventHandler(lnkDel_Command);
                this.Controls.Add(Item);
            }
            this.Items = lstItems.ToArray();

        }
        public ECommerce.Model.Productos.Package[] PackagesSeleccionadas
        {
            get
            {
                ECommerce.Model.Productos.Package Package;
                List<ECommerce.Model.Productos.Package> result = new List<ECommerce.Model.Productos.Package>();
                foreach (ECommerce.WebControls.ObjetoListItem Item in this.Items)
                {
                    if (Item.Checked)
                    {
                        Package = new ECommerce.Model.Productos.Package();
                        Package.Load(Convert.ToInt32(Item.Value));
                        result.Add(Package);
                    }
                }
                return result.ToArray();
            }
        }
        public event System.Web.UI.WebControls.CommandEventHandler lnkDelCommand;
        public event System.Web.UI.WebControls.CommandEventHandler lnkAddCommand;
        void lnkDel_Command(object sender, System.Web.UI.WebControls.CommandEventArgs e)
        {
            lnkDelCommand(sender, e);
        }
        void lnkAdd_Command(object sender, System.Web.UI.WebControls.CommandEventArgs e)
        {
            lnkAddCommand(sender, e);
        }
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
        }
        protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {
            RenderContents(writer);
        }
    }
}
